package com.qianfeng.rencai.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by wenka on 2022/3/30 21:50
 *
 * @author wenkaixin
 * @version 1.0
 * @since 1.0
 */
@Controller
public class CaptchaController {
    public static final String CAPTCHA_SESSION_KEY = "captcha";

    /**
     * 登录页面生成验证码
     */
    @GetMapping(value = "/captcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response,@RequestParam("t") String radomMiss) throws IOException {
        response.setContentType("image/jpeg");
        //自定义验证码图形大小
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(150, 50);
        request.getSession().setAttribute(CAPTCHA_SESSION_KEY,lineCaptcha.getCode());
        ServletOutputStream outputStream = response.getOutputStream();
        lineCaptcha.write(outputStream);
    }
}





